In [5]:
for x in range(10):
print("10" + 3)
pass
In [6]:
bool("")
Out[6]:
In [7]:
choice = ""
bool(choice)
Out[7]:
In [9]:
choice = "N"
bool(choice[0] == "N")
Out[9]:
In [11]:
choice = ""
bool(choice is False)
Out[11]:
In [12]:
bool(choice == False)
Out[12]:
In [13]:
l2 = [1,2,3,10,100,200,4, 8]
l3 = [ x*2 for x in l2]
In [14]:
l3
Out[14]:
In [25]:
def rdouble(x):
return x*2
l3_map = map(rdouble, l2)
l3_map
Out[25]:
In [20]:
for i in range(10):
pass
print("Ciao {}".format(i))
In [22]:
int(3*2)
Out[22]:
In [23]:
map(int(3*2), l2)
In [24]:
int(3*2)
Out[24]:
In [26]:
x
Out[26]:
In [27]:
# map(rdouble, l2)
for z in l2:
rdouble(z)
# map(int(y*2), l2)
for z in l2:
int(y*2)(z)
In [28]:
# Per fare qualcosa di simile a map(y*2, l2)
# Come facciamo? Utilizzare lambda
# def rdouble(x):
# return x*2
# In notazione lambda è
# rdouble = lambda x: x*2
map(lambda x: x*2, l2)
Out[28]:
In [29]:
rdouble = lambda x: x*2
rdouble(10)
Out[29]:
In [30]:
dict(name="Luca")
Out[30]:
In [34]:
raw_input("Vuoi continuare [Y/n]? ").upper() != "N"
Out[34]:
In [ ]:
break
while True:
print("(RI)COMINCIO")
while True:
a = raw_input("Vuoi continuare [Y/n]? ").upper()
if a in ["Y", "N"]:
break
if a == "N":
print("esco")
break
In [9]:
# Scoping delle variabili
# http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules
# http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/
print("ciao")
def funca():
y = 0
while True:
while True:
x = 3
if y < 2:
y += 1
else:
break
if y >= 2:
break
print(x, y)
funca()
print(y)
In [10]:
len(xrange(10))
Out[10]:
In [23]:
import json
def get_json(data):
return json.dumps(data, indent=2)
In [25]:
d = {"name": 'Lu""ca', "city": "Fabriano"}
print(get_json([d]))
In [21]:
str(d)
Out[21]:
In [17]:
for x in PEOPLE:
print("{name},{city}".format(**x))
In [28]:
s.upper(), s.index("a"), s.startswith("B")
Out[28]:
In [29]:
s.__len__()
Out[29]:
In [30]:
len(s)
Out[30]:
In [31]:
len(s) -> s.__len__()
a + b -> a.__add__(b)
In [35]:
class Greeter(object):
def __init__(self, name):
self.name = name
def hello(self):
print("Hello {0.name}".format(self))
luca = Greeter("Luca")
luca.hello()
simone = Greeter("Simone")
simone.hello()
In [ ]: